package tgc.edu.xzy.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import tgc.edu.xzy.entity.SysUser;
import tgc.edu.xzy.repostory.SysUserRepository;
import tgc.edu.xzy.security.UserUtils;
import tgc.edu.xzy.service.LogService;

@Controller
public final class LoginController {
	@Autowired
	private UserUtils userUtils;
	@Autowired
	private LogService logDAO;
	@Autowired
	private SysUserRepository userDAO;
	
	@RequestMapping(value= {"/login","/"},method=RequestMethod.GET)
	public String login() {
		return "login";
	}
	
	@RequestMapping(value="welcome")
	public String welcome() {
		return "welcome";
	}
	
	@RequestMapping(value="/update")
	public String update() {
		return "User/update";
	}
	
	@RequestMapping(value="/xg")
	public String xg(String pad) {
		SysUser user = userUtils.getUser();
		BCryptPasswordEncoder encoder=new BCryptPasswordEncoder();
		user.setPassword(encoder.encode(pad));
		userDAO.save(user);
		return "redirect:logout";
	}
	
	@RequestMapping(value="/pwd")
	@ResponseBody
	public Boolean pwd(String password) {
		SysUser user = userUtils.getUser();
		BCryptPasswordEncoder encoder=new BCryptPasswordEncoder();
		boolean f = encoder.matches(password,user.getPassword());
		return f;
	}
}
